package com.morehair.service.admin.impl;

import com.github.pagehelper.PageInfo;
import com.morehair.bean.bo.admin.BaseReq;
import com.morehair.bean.bo.admin.CouponReq;
import com.morehair.bean.pojo.Coupon;
import com.morehair.bean.pojo.CouponExample;
import com.morehair.bean.pojo.CouponUser;
import com.morehair.bean.pojo.CouponUserExample;

import com.morehair.bean.vo.admin.BaseData;
import com.morehair.bean.bo.admin.CouponUserReq;
import com.morehair.mapper.CouponMapper;
import com.morehair.mapper.CouponUserMapper;
import com.morehair.service.admin.CouponService;
import com.morehair.utils.AdPageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author: DLX
 * @Date: 2021/6/5 21:49
 * @Description:
 **/
@Service
@Transactional
public class CouponServiceImpl implements CouponService {

    @Autowired
    CouponMapper couponMapper;

    @Autowired
    CouponUserMapper couponUserMapper;

    @Override
    public BaseData<Coupon> query(BaseReq baseReq) {
        AdPageHelper.startPage(baseReq);

        CouponReq couponReq = (CouponReq) baseReq;
        String name = couponReq.getName();
        Short status = couponReq.getStatus();
        Short type = couponReq.getType();

        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();

        criteria.andDeletedEqualTo(false);
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (type != null) {
            criteria.andTypeEqualTo(type);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }

        List<Coupon> coupons = couponMapper.selectByExample(couponExample);
        PageInfo<Coupon> couponPageInfo = new PageInfo<>(coupons);
        long total = couponPageInfo.getTotal();

        return BaseData.ok(coupons, total);
    }

    @Override
    public Coupon selectById(Integer id) {
        Coupon coupon = couponMapper.selectByPrimaryKey(id);
        return coupon;
    }

    @Override
    public BaseData<CouponUser> queryCouponUserList(BaseReq baseReq) {
        AdPageHelper.startPage(baseReq);

        CouponUserReq couponUserReq = (CouponUserReq) baseReq;
        Integer couponId = couponUserReq.getCouponId();
        Integer userId = couponUserReq.getUserId();
        Short status = couponUserReq.getStatus();

        CouponUserExample couponUserExample = new CouponUserExample();
        CouponUserExample.Criteria criteria = couponUserExample.createCriteria();

        criteria.andDeletedEqualTo(false);
        if (couponId != null) {
            criteria.andCouponIdEqualTo(couponId);
        }
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }


        List<CouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);
        PageInfo<CouponUser> couponUserPageInfo = new PageInfo<>(couponUsers);
        long total = couponUserPageInfo.getTotal();

        return BaseData.ok(couponUsers, total);
    }

    @Override
    public Coupon create(Coupon coupon) {
        Date date = new Date();
        Date addTime = date;
        Date updateTime = date;
        coupon.setAddTime(addTime);
        coupon.setUpdateTime(updateTime);


        if (coupon.getType() == 1 || coupon.getType() == 2) {// 注册券和兑换券不显示
            coupon.setDeleted(true);
        }
        int i = couponMapper.insertSelective(coupon);
        if (i == 1) {
            Coupon newCoupon = couponMapper.selectNewCoupon();
            return newCoupon;
        }
        return null;
    }

    @Override
    public Coupon couponUpdate(Coupon coupon) {
        Date date = new Date();
        Date updateTime = date;
        coupon.setUpdateTime(updateTime);
        int i = couponMapper.updateByPrimaryKeySelective(coupon);
        if (i == 1) {
            Coupon updatedCoupon = couponMapper.selectByPrimaryKey(coupon.getId());
            return updatedCoupon;
        }
        return null;
    }

    @Override
    public int couponDelete(Coupon coupon) {
        coupon.setDeleted(true);// 删除不可见
        coupon.setStatus((short) 3);// 删除即下架
        int affectedRows = couponMapper.updateByPrimaryKeySelective(coupon);
        if (affectedRows == 1) {
            return 200;
        }
        return 404;
    }

}
